<?php

namespace app\push\controller;


use Channel\Client;
use think\worker\Server;

use Channel\Server as Channel_Server;

class On extends Server
{
    protected $socket = "websocket://0.0.0.0:9999";
    protected $option = ['name'=>'on','count'=>3];

    public function onConnect($connect)
    {
        $connect->send('connection_id='.$connect->id.' worker_id='.$this->worker->id);
    }

    public function onWorkerStart($worker){

      Client::connect("127.0.0.1",12138);

      //单个用户订阅事件
       echo "====".$worker->id."===".PHP_EOL;
      Client::on($worker->id,function ($data) use($worker){
          $connection = $worker->connections[$data['connection_id']];
          $connection->send($data['content']);
      });

        //所有网站链接用户的订阅事件
        Client::on('broadcast',function($data)use($worker){
            //$data = ['content'=>'欢迎登陆'];
            foreach($worker->connections as $conn){
                $conn->send($data['content']);
            }
        });
    }
}

new Channel_Server("0.0.0.0",12138);
